package com.csun.cmny.provider.controller;

import com.csun.cmny.core.annotation.LogAnnotation;
import com.csun.cmny.core.annotation.ValidateAnnotation;
import com.csun.cmny.core.support.BaseController;
import com.csun.cmny.provider.model.dto.RoomSortQueryDto;
import com.csun.cmny.provider.model.dto.RoomSortSaveDto;
import com.csun.cmny.provider.model.vo.RoomSortVo;
import com.csun.cmny.provider.resolver.CmnyMessageSource;
import com.csun.cmny.provider.service.OmcRoomSortService;
import com.csun.cmny.util.wrapper.WrapMapper;
import com.csun.cmny.util.wrapper.Wrapper;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @Auther: chisj chisj@foxmal.com
 * @Date: 2019-03-11 13:13
 * @Description: the class omc room sort controller
 */
@RestController
@RequestMapping(value = "/room", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class OmcRoomSortController extends BaseController {

    @Resource
    private CmnyMessageSource cmnyMessageSource;

    @Resource
    private OmcRoomSortService omcRoomSortService;

    /**
     * 根据楼栋Id查询房间排序
     *
     * @param roomSortQueryDto  room sort query dto
     * @param br                binding result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/queryRoomSort")
    public Wrapper queryRoomSort(@Validated @RequestBody RoomSortQueryDto roomSortQueryDto,
                                 BindingResult br) {

        RoomSortVo roomSortVo = omcRoomSortService.queryRoomSort(roomSortQueryDto);

        //return WrapMapper.ok(roomSortVo);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),roomSortVo);
    }

    /**
     * 保存房间排序
     *
     * @param roomSortSaveDto   room sort save dto
     * @param br                binging result
     *
     * @return  the wrapper.
     */
    @ValidateAnnotation
    @LogAnnotation(isSaveRequestData = true, isSaveResponseData = true)
    @PostMapping(value = "/saveRoomSort")
    public Wrapper saveRoomSort(@Validated @RequestBody RoomSortSaveDto roomSortSaveDto, BindingResult br) {

        Integer result = omcRoomSortService.saveRoomSort(roomSortSaveDto, getLoginAuthDto());
        if (result < 1) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, cmnyMessageSource
                    .getMessage("operation.failed", null));
        }

        //return WrapMapper.ok(result);
        return WrapMapper.wrap(Wrapper.SUCCESS_CODE, cmnyMessageSource
                .getMessage("operation.success", null),result);
    }

}
